//
//  YPBaseTableViewController.m
//  project


//
//  Created by Jtg_yao on 2018/10/19.
//  Copyright © 2018年 rocHome. All rights reserved.
//

#import "YPBaseTableViewController.h"

#define REQUEST_PAGE_SIZE 15

@interface YPBaseTableViewController ()

@property (nonatomic,assign) UITableViewStyle style;

@end

@implementation YPBaseTableViewController

-(instancetype)initWithStyle:(UITableViewStyle)style {
    if (self == [super init]) {
        self.style = style;
    }
    return self;
}

-(instancetype)init {
    if (self == [super init]) {
        self.style = UITableViewStylePlain;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self BaseSubViewInit];
}

- (void)BaseSubViewInit {
    
    [self.view addSubview:self.tableView];
    
    WeakSelf();
    [self.tableView setPullDownBlock:^(YPBaseTableView *currentTable) {
        weakSelf.page = @1;
        [weakSelf requestDataSource];
    } withPullUpBlock:^(YPBaseTableView *currentTable) {
        weakSelf.page = @(weakSelf.page.integerValue + 1);
        [weakSelf requestDataSource];
    }];
    
    self.tableView.emptyTapBlock = ^(YPBaseTableView *currentTable) {
        [weakSelf requestDataSource];
    };
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.left.right.mas_equalTo(self.view);
    }];
}

- (void)requestDataSource {
    
    [YPProgressHUD showDIYLoading];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.tableView.emptyStatus = YPPageEmptyStatusHidden;
        [YPProgressHUD dismissHUD];
        [self.tableView pullRefreshEnd];
        [self.tableView reloadData];
    });
}


#pragma mark -- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YPBaseTableViewCell *cell = [YPCellFactory createCellWithModel:[YPBaseModel new]tableView:tableView indexPath:indexPath];
    return cell;
}

#pragma mark -- UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -- Getter
-(YPBaseTableView *)tableView {
    
    if (_tableView == nil) {
        _tableView = [[YPBaseTableView alloc] initWithFrame:CGRectZero style:self.style isPullUp:YES isPullDown:YES];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.backgroundColor = self.backColor;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
        
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 44.0f;
        
        if (isIOS11Later) { // ios 11 以上
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
            _tableView.estimatedSectionHeaderHeight   = 0;
            _tableView.estimatedSectionFooterHeight   = 0;
        }
    }
    return _tableView;
}

-(NSMutableArray *)dataList {
    
    if (_dataList == nil) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

-(NSNumber *)page {
    
    if (_page == nil) {
        _page = [NSNumber numberWithInteger:1];
    }
    return _page;
}

-(NSNumber *)pageSize {
    if (_pageSize == nil) {
        _pageSize = [NSNumber numberWithInteger:REQUEST_PAGE_SIZE];
    }
    return _pageSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
